import 'dart:convert';
import 'package:flutter/material.dart';
import '../widget/reminder/toasty.dart';
import '../widget/reminder/top_reminder.dart';
import '../index.dart';


mixin NetCancel<T extends StatefulWidget> on State<T> {
  BuildContext _contextBaseFunction;

  @override
  void initState() {
    super.initState();
    _contextBaseFunction = context;
  }

  String _formatObj2Str(Object obj) {
    String content;
    if (obj is Map) {
      content = json.encode(obj);
    } else if (obj is String) {
      content = obj;
    } else {
      content = "$obj";
    }
    return content;
  }

  /// 包含类名的打印
  void log(Object content, {String tag}) {
    tag ??= getClassName() + "-- ";
    String msg = _formatObj2Str(content);
    print(tag + msg);
  }

  ///信息太长,分段打印
  void logLong(Object content, {String tag}) {
    tag ??= getClassName() + "-- ";
    String msg = _formatObj2Str(content);
    //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
    var maxStrLength = 500 - tag.length;

    while (msg.length > maxStrLength) {
      log(msg.substring(0, maxStrLength));
      msg = msg.substring(maxStrLength);
    }
    //剩余部分
    log(msg);
  }

  ///弹对话框
  void showToastDialog(String message,
      {String title = "提示", String negativeText = "确定", Function callBack}) {
    if (_contextBaseFunction != null) {
      if (message != null && message.isNotEmpty) {
        showDialog<Null>(
            context: _contextBaseFunction, //BuildContext对象
            barrierDismissible: false,
            builder: (BuildContext context) {
              return new MessageDialog(
                title: title,
                negativeText: negativeText,
                message: message,
                onCloseEvent: () {
                  Navigator.pop(context);
                  if (callBack != null) {
                    callBack();
                  }
                },
              );
              //调用对话框);
            });
      }
    }
  }

  ///获取实例名
  String getInstanceName() {
    if (_contextBaseFunction == null) {
      return null;
    }
    String instanceName = _contextBaseFunction.toString();
    if (instanceName == null) {
      return null;
    }
    return instanceName;
  }

  ///获取类名
  String getClassName() {
    if (_contextBaseFunction == null) {
      return null;
    }
    String className = _contextBaseFunction.toString();
    if (className == null) {
      return null;
    }
    if (className.indexOf("(") != -1) {
      className = className.substring(0, className.indexOf("("));
    }
    return className;
  }

  ///弹出吐司
  void showToast(String msg,
      {int duration = 2,
      int gravity = 0,
      Color backgroundColor = Colors.black54,
      Color textColor = Colors.white,
      double backgroundRadius = 20}) {
    Toasty.show(_contextBaseFunction, msg,
        duration: duration,
        gravity: gravity,
        backgroundColor: backgroundColor,
        textColor: textColor,
        backgroundRadius: backgroundRadius);
  }

  ///顶部提示
  void showTopReminder(
    String content, {
    Color bgColor = Colors.black54,
    Color textColor = Colors.white,
    int duration = 3,
    double height = 90.0,
    double textSize = 18.0,
  }) {
    if (content != null) {
      if (content != null && content.isNotEmpty) {
        TopReminder.openTopReminder(
            _contextBaseFunction,
            TopReminder(
              reminderText: content,
              bgColor: bgColor,
              textColor: textColor,
              height: height,
              textSize: textSize,
            ));
      }
    }
  }

  /// 显示网络请求加载框
  void setLoadingWidgetVisible(bool isShow, {String tip = ''}) {
    showDialog(
        context: _contextBaseFunction,
        barrierDismissible: false,
        builder: (context) {
          return new NetCustomLoadingDialog(tip);
        });
  }

  /// 关闭加载框
  void setLoadingWidgetDismiss(bool bool, {String tip}) {
    Navigator.pop(_contextBaseFunction);
  }
}
